<%@page import="java.util.List"%>
<%@page import="database.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>

<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="/Project1/layui-v2.6.13/layui/layui.js"></script>
		<script type="text/javascript" src="/Project1/js/jquery-1.9.1.min.js"></script>
		<link rel="stylesheet" type="text/css" href="/Project1/layui-v2.6.13/layui/css/layui.css"/>
		<style>
			#limitPage button{
				margin: 0;
			}
			
		</style>
	</head>
	<body>
	<%
	int pagen;
	int num = 3;
	int len;
	List<Student> stulist;
	List<College> colleges = DP.update(College.class, College.sqlSel(), null);
	List<Major> majors = DP.update(Major.class, Major.sqlSel(), null);
	String sql;
	if(request.getAttribute("reset")!=null){
		pagen = 1;
		sql = "select * from student";
		len = DP.update(Student.class, sql, null).size();
		stulist = DP.update(Student.class, sql+" limit 0,3", null);
		
	}
	else{
		if(request.getAttribute("sql")!=null){
			sql = (String)request.getAttribute("sql");
			stulist = DP.update(Student.class, sql, null);
			pagen = (Integer)request.getAttribute("page");
			len = (int)request.getAttribute("len");
		}
		else{
		pagen = 1;
		sql = "select * from student";
		len = DP.update(Student.class, sql, null).size();
		stulist = DP.update(Student.class, sql+" limit 0,3", null);
		}
	}
	int sum = len%num==0?len/num:len/num+1;
	System.out.println("sum is "+sum);
	if(pagen < 1) pagen = 1;
	System.out.println("this is "+sql);
	request.setAttribute("stulist", stulist);
	request.setAttribute("sql", sql);
	request.setAttribute("page", pagen);
	request.setAttribute("num", num);
	request.setAttribute("cs", colleges);
	request.setAttribute("ma", majors);
	%>
	<script type="text/javascript">
	</script>
		<div style="padding: 20px;" class="all">
			<div >
				<form class="layui-form" action="<%=request.getContextPath()%>/s" method="post">
				<input type="hidden" name="method" value="updatepage">
					<div class="layui-inline">
						<input name="name" type="text" class="layui-input" placeholder="请输入学生姓名">
					</div>
					
					<div class="layui-inline">
						<select name="grade">
							<option value="-1" selected="selected">年级</option>
							<option value="2019" <c:if test="${cgrade==2019}">selected</c:if>>2019</option>
							<option value="2018" <c:if test="${cgrade==2018}">selected</c:if>>2018</option>
							<option value="2017" <c:if test="${cgrade==2017}">selected</c:if>>2017</option>
							<option value="2016" <c:if test="${cgrade==2016}">selected</c:if>>2016</option>
							<option value="2015" <c:if test="${cgrade==2015}">selected</c:if>>2015</option>
						</select>
					</div>
					<div class="layui-inline">
						<select name="c_id" lay-filter="s1">
							<option value="-1" selected="selected">学院</option>
							<c:forEach items="${cs}" var="obj">
								<option value="${obj.id }" <c:if test="${cc_id==obj.id}">selected</c:if>>${obj.name}</option>
							</c:forEach>
						</select>
					</div>
					<div class="layui-inline">
						<select name="m_id" lay-filter="s2" id="major">
							<option value="-1" selected="selected">专业</option>
							<c:choose>
							<c:when test="${!empty cc_id}">
							<c:forEach items="${ma}" var="obj">
								<c:if test="${cc_id==obj.c_id }">
								<option value="${obj.id }" <c:if test="${cm_id==obj.id}">selected</c:if> >${obj.name}</option>
								</c:if>
							</c:forEach>
							</c:when>
							<c:otherwise>
							<c:forEach items="${ma}" var="obj">
								<option value="${obj.id }" <c:if test="${cm_id==obj.id}">selected</c:if> >${obj.name}</option>
							</c:forEach>
							</c:otherwise>
							</c:choose>
						</select>
					</div>
					<input type="submit" value="查询" class="layui-btn">
					<input type="button" value="重置" class="layui-btn" onclick="chong()">
				</form>
			</div>

			<div style="margin-top:20px;">
				<button class="layui-btn" onclick="openLayer1()">添加</button>
				<button class="layui-btn">导出表格</button>
				<table class="layui-table table">
					<colgroup>
						<col width="100">
						<col width="60">
						<col width="60">
						<col width="200">
						<col width="100">
						<col width="60">
						<col width="250">
						<col width="100">
						<col width="120">
						<col width="100">
						<col width="80">
						<col>
					</colgroup>
					<thead>
						<tr>
							<th>姓名</th>
							<th>性别</th>
							<th>年级</th>
							<th>学院</th>
							<th>专业</th>
							<th>班级</th>
							<th>就业单位</th>
							<th>就业城市</th>
							<th>岗位</th>
							<th>企业类型</th>
							<th>薪资</th>
							<th>操作</th>
						</tr> 
					</thead>
					<tbody class="tbody">
						<c:forEach items="<%=stulist %>" var="st">
							<tr id="${st.id }">
								<td>${st.name}</td>
								<td>
								<c:if test="${st.sex eq 1}">男</c:if>
								<c:if test="${st.sex eq 0}">女</c:if>
								</td>
								<td>${st.grade}</td>
								<td>
								<c:forEach items="${cs}" var="obj">
									<c:if test="${st.c_id eq obj.id}">${obj.name}</c:if>
								</c:forEach>
								</td>
								<td>
								<c:forEach items="${ma}" var="obj">
									<c:if test="${st.m_id eq obj.id}">${obj.name}</c:if>
								</c:forEach>
								</td>
								<td>${st.classs}</td>
								<td>
								${st.company }
								</td>
								<td>${st.city}</td>
								<td>${st.job}</td>
								<td>
									<c:if test="${st.type eq 1}">私企</c:if>
									<c:if test="${st.type eq 2}">国企</c:if>
									<c:if test="${st.type eq 3}">集体企业</c:if>
									<c:if test="${st.type eq 4}">事业单位</c:if>
									<c:if test="${st.type eq 5}">科研院所</c:if>
								</td>
								<td>${st.money}</td>
								<td><button class="layui-btn" onclick="openLayer2(${st.id})">更新</button></td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
			</div>
			<div id="limitPage" align="center">
				<button class="layui-btn layui-btn-sm layui-btn-primary layui-border-green" onclick="first()">首页</button>
				<button class="layui-btn layui-btn-sm" onclick="prev()">上一页</button>
				<span class="layui-font-16" >共<%=sum %>页/第<%=pagen %>页</span>
				<button class="layui-btn layui-btn-sm" onclick="next()">下一页</button>
				<button class="layui-btn layui-btn-sm layui-btn-primary layui-border-green" onclick="last()">尾页</button>
			</div>
		</div>
	</body>
	<script>
		function chong(){
			console.info("点一次");
			window.location.href="<%=request.getContextPath()%>/s?method=updatepage&reset=reset";
		}	
	
		function first(){
			window.location.href = "<%=request.getContextPath()%>/s?method=updatepage&sql="+"${sql}"+"&page=1"+"&num="+"${num}";
		}
		function last(){
			window.location.href = "<%=request.getContextPath()%>/s?method=updatepage&sql="+"${sql}"+"&page="+<%=sum%>+"&num="+"${num}";
		}
		function prev(){
			var t = <%=pagen%>
			if(t==1){
			window.location.href = "<%=request.getContextPath()%>/s?method=updatepage&sql="+"${sql}"+"&page="+${page}+"&num="+"${num}";
			}
			else{
			window.location.href = "<%=request.getContextPath()%>/s?method=updatepage&sql="+"${sql}"+"&page="+${page-1}+"&num="+"${num}";	
			}
			
		}
		function next(){
			var t = <%=pagen%>
			if(t>=<%=sum%>){
			window.location.href = "<%=request.getContextPath()%>/s?method=updatepage&sql="+"${sql}"+"&page="+${page}+"&num="+"${num}";
			}
			else{
			window.location.href = "<%=request.getContextPath()%>/s?method=updatepage&sql="+"${sql}"+"&page="+${page+1}+"&num="+"${num}";
			}
		}
		layer.config({
			  path: '/Project1/layui-v2.6.13/layui' //layer.js所在的目录，可以是绝对目录，也可以是相对目录
			});
		function openLayer1(){
			layer.open({
			  type: 2, 
			  area: ['650px', '500px'],
			  content: '<%=request.getContextPath()%>/page/stuinse.jsp',
			  end: function(){
				  location.reload();
			  }
			});
		}
		
		function openLayer2(id){
			layer.open({
			  type: 2, 
			  area: ['650px', '500px'],
			  content: '<%=request.getContextPath()%>/page/studo.jsp?id='+id,
			  end: function () {
				  location.reload();
			  }
			});
			
		}
		
		layui.use('form', function(){
			var form = layui.form;
			
			//select绑定触发事件 on('select(lay-filter)',function(){})
			form.on('select(s1)', function(data){
				var id = data.value;//获取学院id
				console.info(id);
				//jquery的ajax发送HTTP请求,有回调函数：接收服务端返回的数据
				//注意！！！如果请求一个jsp页面，不能用ajax;只用用超链接发送请求
				$.ajax({
					url:"<%=request.getContextPath()%>/s",//请求路径
					type:"post",//请求方式get/post
					data: "method=selectM&c_id="+id,//请求发送的数据{"method":"ms", "collegeId":id} json格式
					//请求发送成功的回调函数
					success:function(obj){
						//将json字符串转为数组
						console.info('成功回调');
						var arr = JSON.parse(obj);
						
						//清空子元素
						$("#major").empty();
						$("#major").append("<option value='-1' selected='selected'>专业</option>")
						//添加子元素
						$.each(arr,function(index, item){
							console.info(item);
							//在元素内部末尾插入子元素
							$("#major").append("<option value='"+item.id+"'>"+item.name+"</option>");
						});
						//重新渲染所有select下拉框
						form.render('select');
					}
				});
			});
		});
	</script>
</html>
